﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Login2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //为密码添加回车事件
        ipt_Pwd.Attributes.Add("onkeydown", "if(event.keyCode==13) { document.all." + Btn_Submit.ClientID + ".focus();document.all." + Btn_Submit.ClientID + ".click();}");
        if (!IsPostBack)
        {
            Response.Cookies.Add(new HttpCookie("CheckCode", ""));

            HttpRequest request = HttpContext.Current.Request;
            if (request != null)
            {
                HttpCookie cookie = request.Cookies["LoginedUser"];
                if (cookie != null)
                {
                    this.ipt_Name.Value = cookie.Value;
                    this.ipt_Name.Focus();
                }
            }

            Session["poptime"] = "";
            Session["waitwork"] = 0;
            Session["newmail"] = 0;
            this.Session["g_user_id"] = null;
        }
    }

    public void Login(object sender, EventArgs e)
    {
        if (CheckUserInfo())
        {
            HttpCookie cookie = new HttpCookie("LoginedUser", this.ipt_Name.Value.Trim());
            cookie.Expires = DateTime.Now.AddDays(30);
            Response.Cookies.Add(cookie);

            SetSingleLoginCookie();

            ControlLoginPage();
        }
        else
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "<script>alert(\"用户名或密码错误！\");</script>");
    }

    private void SetSingleLoginCookie()
    {
        HttpCookie cookie = Response.Cookies.Get("OPERATORID");
        if (cookie == null)
        {
            DataTable dt = CommonLib.BLL.SYS_USER.GetList("user_id='" + this.ipt_Name.Value.Trim() + "'").Tables[0];
            foreach (DataRow dr in dt.Select())
            {
                HttpCookie cookie_operaterid = new HttpCookie("OPERATORID", dr["OTHER_USER1"].ToString());
                cookie_operaterid.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(cookie_operaterid);

                break;
            }

        }
        else
        {
            DataTable dt = CommonLib.BLL.SYS_USER.GetList("user_id='" + this.ipt_Name.Value.Trim() + "'").Tables[0];
            foreach (DataRow dr in dt.Select())
            {
                cookie.Expires = DateTime.Now.AddDays(30);
                cookie.Value = dr["OTHER_USER1"].ToString();

                break;
            }
        }
    }

    /// <summary>
    /// 验证用户名和密码
    /// </summary>
    private bool CheckUserInfo()
    {
        DataSet ds = CommonLib.BLL.SYS_USER.CheckUserData(this.ipt_Name.Value.Trim(), this.ipt_Pwd.Value.Trim());
        if (ds.Tables.Count == 0 || ds.Tables[0].Rows.Count != 1)
            return false;
        DataTable tmp_table = ds.Tables[0];
        if (tmp_table.Rows[0]["user_id"].ToString() != this.ipt_Name.Value.Trim() || CommonLib.ComMon.StringCommon.StringDecode(tmp_table.Rows[0]["user_password"].ToString()) != this.ipt_Pwd.Value.Trim())
            return false;
        this.Session["g_user_id"] = tmp_table.Rows[0]["user_id"].ToString();
        this.Session["g_user_name"] = tmp_table.Rows[0]["user_name"].ToString();
        this.Session["g_dept_name"] = tmp_table.Rows[0]["dept_name"].ToString();
        this.Session["g_dept_id"] = tmp_table.Rows[0]["dept_id"].ToString();
        this.Session["g_yxuser_id"] = tmp_table.Rows[0]["other_user1"].ToString();
        this.Session["workNo"] = tmp_table.Rows[0]["other_user2"].ToString();
        this.Session["g_sys_parameter"] = CommonLib.BLL.SYS_PARAMETER.GetList("1=1").Tables[0];

        return true;
    }

    private void ControlLoginPage()
    {
        var user = CommonLib.BLL.SYS_USER.GetModel(this.Session["g_user_id"].ToString());
        if (user.SUB_SYSTEM < 1)
            Response.Redirect("MainPage.aspx");
        else
        {
            var model = CommonLib.BLL.SYS_SUBSYSTEM.GetModel((short)user.SUB_SYSTEM);
            if (model == null)
                Response.Redirect("MainPage.aspx");
            else
            {
                Response.Redirect("MainFrame.aspx?subsystem=" + model.SUB_SYSTEM + "&subsysname="+model.SYSTEM_NAME);
            }
        }
    }
}
